package com.yb.guigu.pick.minio.upload.config;

import com.yb.guigu.pick.minio.upload.annotation.ConditionalOnUploadClientEnabled;
import com.yb.guigu.pick.minio.upload.bean.MinioProperties;
import com.yb.guigu.pick.minio.upload.client.MinioUploadClient;
import com.yb.guigu.pick.upload.client.UploadClient;
import com.yb.guigu.pick.upload.config.UploadClientAutoConfiguration;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/11 11:25
 * FileName: MinioUploadAutoConfiguration
 * Description:
 */

@Configuration
@EnableConfigurationProperties(MinioProperties.class)
@ConditionalOnUploadClientEnabled
@ConditionalOnProperty(value = "guigu.pick.minio.upload-client.enabled",havingValue = "true",matchIfMissing = true)
@AutoConfigureAfter(UploadClientAutoConfiguration.class)
@ConditionalOnBean(UploadClientAutoConfiguration.class)
@Slf4j
public class MinioUploadAutoConfiguration {

    @Bean
    @Autowired(required = false)
    UploadClient minioUploadClient(MinioProperties minioProperties, MinioClient minioClient){
            return new MinioUploadClient(minioProperties,minioClient);
    }

    @Bean
    @Autowired(required = false)
    MinioClient minioClient(MinioProperties minioProperties){
        log.info("minioProperties:{}",minioProperties);
        MinioClient minioClient = null;
        try {
            minioClient =
                    MinioClient.builder()
                            .endpoint(minioProperties.getEndpoint())
                            .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                            .build();
            initBucket(minioClient,minioProperties.getFileBucket().getName());
            initBucket(minioClient,minioProperties.getVideoBucket().getName());
        }catch (Exception e){
            log.info("create MinioClient error : {}",e);
        }

        log.info("minioClient:{}",minioClient);
        return minioClient;
    }

    public void initBucket(MinioClient minioClient,String bucketName){
        try{
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());

                log.info("Make a new bucket called '{}'.",bucketName);
            } else {
                log.info("Bucket '{}' already exists.",bucketName);
            }
        }catch (Exception e){
            log.info("init Bucket error:{}",e);
        }
    }
}
